<template>
  <div class="gitee-callback-container">
    <div class="loading-box">
      <el-icon class="loading-icon"><Loading /></el-icon>
      <p>正在处理Gitee登录...</p>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Loading } from '@element-plus/icons-vue'


const route = useRoute()
const router = useRouter()



onMounted(() => {
  console.log('GiteeCallbackView 组件已挂载')
  console.log('当前路由路径:', route.path)
  console.log('当前路由查询参数:', route.query)
  console.log('当前路由完整信息:', route)
  
  const { code, token, user_id } = route.query
  console.log('Gitee回调参数:', { code, token: !!token, user_id })
  
  // 如果URL中已经包含token和user_id，说明后端已经处理完成并重定向回来了
  if (token && user_id) {
    console.log('检测到后端已处理完成，直接保存登录信息')
    localStorage.setItem('token', token)
    localStorage.setItem('retoken', route.query.re_token || '')
    localStorage.setItem('userInfo', JSON.stringify({
      user_id: user_id,
      username: route.query.username || '',
      email: route.query.email || ''
    }))
    
    ElMessage.success('Gitee登录成功')
    router.push('/')
  } else if (code) {
    console.log('开始处理Gitee登录回调')
    // 直接跳转到后端API，让后端处理重定向
    window.location.href = `http://localhost:8000/api/gitee_callback/?code=${code}`
  } else {
    console.error('缺少必要参数: code')
    console.error('所有参数:', route.query)
    ElMessage.error('未获取到授权码')
    router.push('/login')
  }
})
</script>

<style scoped>
.gitee-callback-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.loading-box {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  text-align: center;
  max-width: 400px;
  width: 100%;
}

.loading-icon {
  font-size: 48px;
  color: #c71d23;
  margin-bottom: 20px;
  animation: spin 1s linear infinite;
}

.loading-box p {
  color: #666;
  font-size: 16px;
  margin: 0;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
